$(function(){
		win_size = {
			width 	: $(window).width(),
			height 	: $(window).height(), 
		};
		main = {
			init : {
				header_height : 40,
				footer_height : 31,
				win : function(){
<<<<<<< HEAD
					var body_width = $('body').width();
=======
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
					var body_height = $('body').height();
					var win_height = $(window).height();
					if (body_height < win_height)
					{
						//设置页脚位置
						$('#footer').css({'top':win_height-main.init.footer_height,'position':'absolute'});
						$('#sub-menu, #login-left #system-info').height(win_height-main.init.header_height-main.init.footer_height);
						//设置更新信息高度
						$('#login-left .system-update').height('47%');
						$('#login-success, #error').height($(window).height() - 71);
					}
					else
					{
						$('#sub-menu, #login-left #system-info').height($('#container').height());
						$('#footer').removeAttr('style');
						//设置更新信息高度
						$('#login-left .system-update').height('47%');
						//设置登录成功高度
						$('#login-success, #error').height($(window).height() - 71);
					}
					//if ($('#container').height() < $(document).height() - 100) $('#container').height($(document).height() - 100);
					//$('#container').height($(window).height() - 100);
					//$('#sub-menu').height($('#container').height());
					//$('#sub-menu').height($('#container').height());
					//$('#error').height($(window).height() - 100);
					//$('#login-left').height($(window).height() - 100);
					//$('#main').height($('#container').height());
				},
				menu_animiate : function(){
					var $menu_active = $('#header-menu-show');
					var $menu = $('#header-menu');
					var $header_username = $('#header-username');
					$menu_active.mouseenter(function(){
						$menu_active.css({'background':'url('+site.style+'i/menu_show.png) no-repeat center center'})
						$menu.height(($menu.find('li').height() + 1) * $menu.find('li').length);
					});
					$menu.mouseleave(function(){
						$menu_active.removeAttr('style');
						$menu.removeAttr('style');
					});
					//用户菜单
					$header_username.mouseenter(function(){
						$('#user-panel').show(0, function(){
							$(this).mouseleave(function(){
								$(this).hide();
							})
						});
					});
				},
				//快速所搜索
				quick_search : function()
				{
					//动画部分
					var $search_input_w = $('#header-quick-search');
					var $search_input_button = $search_input_w.siblings('#header-quick-search-button');
					var $search_input_close = $search_input_w.siblings('#header-quick-search-close');
					var $search_input = $search_input_w.children('input');
					var input_width = $search_input.width();
					$search_input.focus(function(){add_style()})
					.blur(function(){remove_style()});

					//点击所搜按钮
					$search_input_button.click(function(){
						search_main_goods();
					});
					$(window).keydown(function(){
						if (event.keyCode == 13)
						{
							search_main_goods();
						}
					});
					//处理发送请求
					search_main_goods = function()
					{
						text = $search_input.val();
						text = text.replace(/^\s+|\s+$/g,"");
						if ('' == text)
						{
							$search_input.focus();
						}
						else
						{
							if (text.length > 1)
							{
								url = site.url + 'ajax/quick_search';
								json = tool.toJson('{"goods_type":"mg","q":"'+text+'"}');
								style.loading('start');
								$.post(url, json, function(dom){
									remove_search_content();
									$(dom).appendTo('body');
									$search_input_close.show().click(function(){
										remove_search_content();
										$search_input_close.hide();
									});
									tool.popup($('#quick_search_content'), $search_input_w, [38, -67], 'fixed');
									style.loading('end');
								});
							}
							else
							{
								tool.customAlert({'obj':'请输入大于1个字符'});
							}
						}
					}

					//添加样式
					add_style = function()
					{
						$search_input_w.css('background', '#292929');
						$search_input_button.css('background', '#292929');
						$search_input.stop().animate({'width':input_width*1.5}, 150);
					}

					//移除样式
					remove_style = function()
					{
						$search_input_w.removeAttr('style');
						$search_input_button.removeAttr('style');
						if ($search_input.val().length == 0)
						{
							$search_input.stop().animate({'width':input_width}, 150);
						}
					}

					//移除搜索层
					remove_search_content = function()
					{
						$('#quick_search_content').remove();
					}
				}
			},
			formToJson : function($obj, name, elval){
				//name[String]: Json name
				//$obj[jQuery Object]: Where the value from
				data = "{'" + name + "[]':[";
				$obj.each(function(){
					data = data + "'" + $(this).val() + "',";
				})
				data = data.slice(0, -1);
				data = data + "]}";
				return eval('('+data+')');
			},
			doAjax : function(url, data, status, callback) {
				$e = $('<div id="ajax-waiting">执行中</div>');
				$e.appendTo('#header-menu');
				$.post(url, data, function(r){
					if (status ==  r.r) {
						callback(r);
					}
					else {
						$('#main').children().remove();
						$(r).appendTo('#main');
					}
					$('#ajax-waiting').remove();
				}, 'json')
			},
			stock_retext : function() {
				$('div').each(function(){
					if('outbound' == $(this).text()) $(this).text('出库');
					if('inbound' == $(this).text()) $(this).text('入库');
					if('sale' == $(this).text()) $(this).text('销售');
					if('return' == $(this).text()) $(this).text('退货');
					if('other' == $(this).text()) $(this).text('其他');
					if('order' == $(this).text()) $(this).text('进货');
					if('transfer' == $(this).text()) $(this).text('调货');
				})
			},
			store_retext : function($obj) {
				$obj.each(function(){
					if('1' == $(this).text()) $(this).text('主仓');
					if('2' == $(this).text()) $(this).text('副仓');
				});
			}
		};
		tool = {
			set_default : function(argument, value)
			{
				if (typeof(argument) == 'undefined')
				{
					return value;
				}
				else
				{
					return argument;
				}
			},
			getLocalTime : function(nS) {
				date = new Date(parseInt(nS) * 1000);
			    return date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
			},
			toJson : function(string) {
				return JSON.parse(string, function (key, value) {
				    var type;
				    if (value && typeof value === 'object') {
				        type = value.type;
				        if (typeof type === 'string' && typeof window[type] === 'function') {
				            return new (window[type])(value);
				        }
				    }
				    return value;
				});
			},
			formToJson : function($obj, name, open_tag) {
				//name[String]: Json name
				//$obj[jQuery Object]: Where the value from
				if (open_tag)
				{
					data = '"' + name + '[]":[';
				}
				else
				{
					data = '{"' + name + '[]":[';
				}
				$obj.each(function(){
					data = data + '"' + $(this).val() + '",';
				})
				data = data.slice(0, -1);
				if (open_tag)
				{
					data = data + ']';
					return data;
				}
				else
				{
					data = data + ']}';
					return tool.toJson(data);
				}
			},
			mergeJson : function(json1, json2) {  
	            var resultJsonObject={};  
	            for(var attr in json1){  
	                resultJsonObject[attr]=json1[attr];  
	            }  
	            for(var attr in json2){  
	                resultJsonObject[attr]=json2[attr];  
	            }  
	  
	            return resultJsonObject;  
        	},
        	popup : function($obj, $refer_obj, offset, position)
        	{
        		t = 0;
        		l = 0;
        		if ('' != $refer_obj)
        		{
	        		t = $refer_obj.offset().top;
	        		l = $refer_obj.offset().left;
        		}
        		if (undefined !== offset)
        		{
        			t += parseInt(offset[0]);
        			l += parseInt(offset[1]);
        		}
        		if (undefined != position)
        		{
        			$obj.css('position', position);
        			t -= parseInt($(window).scrollTop());
        		}
        		$obj.css({'top':t+'px','left':l+'px'});
        	},
        	jump_box : {
        		init : function($obj)
        		{
	        		//清除jump-box
	        		tool.jump_box.close();
	        		$('#jump-box-content').remove();
	        		$wrapper = $('<div id="jump-box"></div>');
	        		$content = $('<div id="jump-box-content"></div>');
	        		$obj.appendTo($content);
	        		$wrapper.appendTo($('body'));
	        		$content.appendTo($('body'));
        			tool.jump_box.draw();
        		},
        		draw : function()
        		{
	        		width 	 = $(window).width();
	        		height 	 = $(window).height() - 40;
	        		c_width  = $('#jump-box-content > div').width();
	        		c_height = $('#jump-box-content > div').height();
	        		$('#jump-box').css({'width':'100%','height':height,'background':'#000','opacity':0.5,'position':'fixed','top':40});
	        		$('#jump-box-content').css({'position':'fixed','top':38,'left':width/2-c_width/2});
	        		$('#jump-box').click(function(){
	        			tool.jump_box.close();
	        		})
        		},
        		close : function()
        		{
	        		$('#jump-box').remove();
	        		$('#jump-box-content').remove();
        		}
        	},
        	bind_link : function($obj, link)
        	{
        		$obj.unbind('click').bind('click', function(){
        			location.href = link;
        		});
        	},
        	customAlert: function(options, type, callback) {

				var defaults = {
					obj		: 'Default',
					height 	: 300,
					background	: 'blue',
					confirm : false
				};
				var options = $.extend(defaults, options);
				var width;
				var height;
				var func;

				//初始化
				function init()
				{
					//获得可视区域大小
					width = document.documentElement.clientWidth;
					height = document.documentElement.clientHeight;
					//IE特殊待遇
					if (jQuery.browser.msie && 6.0 == jQuery.browser.version)
					{
						width = document.body.offsetWidth;
						height = document.body.offsetHeight;
					}
					//清除已有的对话框
					$('#customAlert').remove();
					$('#customAlert-dialog').remove();
				}

				//添加层
				function pro()
				{
					//初始化遮罩层
					$wrapper = $('<div id="customAlert"></div>');
					$wrapper.width(width).height(height);
					$wrapper.css({'position':'fixed','z-index':'9999','top':'0','left':'0','background':'white','opacity':'0.5'});
					$dialog = $('<div id="customAlert-dialog"></div>');
					$dialog.width(width).height(options.height);
					$dialog.css({'position':'fixed','z-index':'10000','top':height/2-options.height/2+'px','left':'0','background':options.background});
					$wrapper.appendTo('body');
					//对话框类型
					if (undefined == type || 'text' == type)
					{
						$dialog.append('<div id="customAlert-text">' + options.obj + '</div>');
						$dialog.find('#customAlert-text').css({'text-align':'center','line-height':options.height+'px','color':'#fff'});
					}
					else if ('interface' == type)
					{
						func = callback;
						func();
					}
					$dialog.appendTo('body');
					if (options.confirm)
					{
						$action = $('<div id="customAlert-action"><div class="customAlert-action" id="customAlert-cancel">取消</div><div class="customAlert-action" id="customAlert-confirm">确认</div></div>');
						$action.css({'position':'absolute','margin-top':'-40px','margin-left':width/2+750/2-100+'px'});
						$action.find('.customAlert-action').css({'width':'100px','height':'25px','margin-left':'5px','float':'right','background':'#fff','line-height':'25px','text-align':'center','font-size':'12px','cursor':'pointer'})
						$dialog.append($action);
						$('#customAlert-confirm').unbind('click').bind('click', function(){
							callback();
							close();
						});
						$('#customAlert-cancel').unbind('click').bind('click', function(){
							close();
						});
					}
				}
				
				function close()
				{
					$('#customAlert').remove();
					$('#customAlert-dialog').remove();
					//解除时间绑定
					if (options.confirm){
						$('#customAlert-confirm').unbind('click');
						$('#customAlert-cancel').unbind('click');
					}
					$(window).unbind('resize');
				}

				init();
				pro();
				$('#customAlert').click(function(){
					close();
				});

				//绑定窗口变化时间
				$(window).bind('resize', function(){
					//重新初始化
					init();
					//重置层尺寸
					$wrapper.width(width).height(height);
					$dialog.width(width).height(options.height);
					//重置对话框位置
					$dialog.css('top',height/2-options.height/2+'px');
				});

				//清除对话框

				/***
				return this.each(function(){
					
				});
				***/

			},
			set_fixed : function($obj)
			{
				$(window).bind('scroll', function(){
					var scroll_top = $(document).scrollTop();
					if (40 < scroll_top)
					{
						$obj.css({'position':'fixed','top':40});
					}
					else
					{
						$obj.removeAttr('style');
					}
				});
			}
		},
		style = {
			loading : function(status, type){
				if ('footer' == type || !type) {
					if ('start' == status) {
						$('#footer').css('background', 'url(' + site.style + 'i/input_text_loading.gif' + ') repeat-x').addClass('loading');
					}
				}
				if ('end' == status) {
					$('.loading').removeClass('loading').removeAttr('style');
				}
			},
			highline : function($obj, color, relate)
			{
				$obj.bind('mouseenter', function(){
					$obj.css('background', 'none');
					$(this).css('background', color);
				}).bind('mouseleave', function(){
					$obj.css('background', 'none');
				});
			}
		},
		//数据列表动态相关方法
		dataList = {
			init : function(){
				$('#list .each').mouseenter(function(){
					if (!$(this).attr('checked')) {$(this).css('background', '#eee').find('.btn a').css('display', 'block')}
				}).mouseleave(function(){
					if (!$(this).attr('checked')) {$(this).css('background', '#fff').find('.btn a').css('display', 'none')}
				});
				$('#list .each .check').click(function(){
					$('#list .each').css({'background':'#fff','color':'#444'}).removeAttr('checked');
					$(this).parents('#list .each').css('background', '#eee').find('.btn a').css('display', 'none');
					$('#list .each :checked').parents('#list .each').css({'background':'#005eff','color':'#eee'}).attr('checked','checked');
				});
			},
			checkAll : function(){
				if ($('#check-text').attr('checked')) {
					$('#check-text').removeAttr('checked').html('<span>全选</span>');
					$('#list input[type=checkbox]').removeAttr('checked');
					$('#list .each').css({'background':'#fff','color':'#444'}).removeAttr('checked','checked');
				}
				else {
					$('#check-text').attr('checked', 'checked').html('<span>全不选</span>');
					$('#list input[type=checkbox]').attr('checked', 'true');
					$('#list .each').css({'background':'#005eff','color':'#eee'}).attr('checked','checked');
				}
			},
			search : function(){
				s = $('#search input').val().split(':');
				if ('n' == s[0] || 's' == s[0] || 'sn' == s[0] || 'ss' == s[0]){
					url = site.url + 'goods/search/' + s[0] + '/' + s[1];
					location.href = url;
					return false;
				}
				alert('正确的搜索格式：\n[n:number] 货号搜索主货品\n[s:number] 搜索码搜索主货品\n[sn:number] 货号搜索子货品\n[ss:number] 搜索码搜索子货品');
				return false;
			}
		};
		/*********重绘表单样式***********/
		form = {
			init : function(){
				this.text._init();
				this.checkbox._init();
				this.checkbox._action();
				this.select._init();
			},
			text : {
				_init : function(){
					$('.edit-text input:disabled').css('border', '1px solid #ddd');
				}
			},
			checkbox : {
				_init : function(){
					$('.edit-checkbox input[type=checkbox]').addClass('hide').parent('.edit-checkbox').addClass('form-checkbox');
				},
				_action : function(){
					$('.form-checkbox input[type=checkbox]').parent('.form-checkbox').removeClass('form-checkbox-checked').removeAttr('style');
					$('.form-checkbox input[type=checkbox]:checked').parent('.form-checkbox').addClass('form-checkbox-checked').css('color', '#fff');
				}
			},
			select : {
				_init : function($obj){
					$o = $('.edit-select select');
					$o.addClass('hide');
					if ($obj) {
						$obj.next('.form-select').remove();
						$o = $obj;
					}
					if (1 == $o.length) {
						$opts = $o.find('option');
						html = '<div class="form-select">';
						value = $o.find(':selected').val();
						$opts.each(function(){
							if (value == $(this).val()){
								html = html + '<div class="option form-select-selected" value="' + $(this).val() + '">' + $(this).text() + '</div>';
							}
							else{
								html = html + '<div class="option" value="' + $(this).val() + '">' + $(this).text() + '</div>';
							}
						});
						html = html + '</div>';
						$html = $(html).find('.option').addClass('hide').end();
						$html = $html.find('.form-select-selected').removeClass('hide').end();
						$o.after($html);
					}
					else if ($o.length > 1) {
						//alert('表单重绘框架 select暂时只支持单对象');
						$o.each(function(){
							$opts = $(this).find('option');
							html = '<div class="form-select">';
							value = $(this).find(':selected').val();
							$opts.each(function(){
								if (value == $(this).val()){
									html = html + '<div class="option form-select-selected" value="' + $(this).val() + '">' + $(this).text() + '</div>';
								}
								else{
									html = html + '<div class="option" value="' + $(this).val() + '">' + $(this).text() + '</div>';
								}
							});
							html = html + '</div>';
							$html = $(html).find('.option').addClass('hide').end();
							$html = $html.find('.form-select-selected').removeClass('hide').end();
							$(this).after($html);
						});
					}
				},
				_click : function($o, callback, index){
					$p = $o.parents('.form-select');
					if ($o.parents('.form-select-selecting').length == 1) {
						$o.parents('.form-select').removeClass('form-select-selecting');
						$o.addClass('form-select-selected').siblings('.option').removeClass('form-select-selected').slideUp(200);
						$o.parents('.form-select').prev().find('option').removeAttr('selected').end().children('option[value="' + $o.attr('value') + '"]').attr('selected', 'selected');
						if (callback) {
							if (index) {
								for (i=0; i<index.length; i++) {
									if (index[i] == $p.index('.form-select')) {
										callback($o.attr('value'), $p);
									}
								}
							}
							else {
								callback($o.attr('value'), $p);
							}
						}
					}
					else {
						$o.parents('.form-select').addClass('form-select-selecting');
						$o.siblings('.option').slideDown(300);
					}
				}
			}
		};
		ajax = {
			dataList : {
				del : function(url, $obj, name, callback){
					if ($obj.length > 0) {
						data = main.formToJson($obj, name);
						if (!callback) {
							callback = function(){$obj.parents('.each').remove()};
						}
						main.doAjax(url, data, 'DEL_SUCCESS', callback);
					}
					else {
						alert('没有选取');
					}
					return false;
				},
				recover : function(url, $obj, name, callback){
					if ($obj.length > 0) {
						data = main.formToJson($obj, name);
						if (!callback) {
							callback = function(){$obj.parents('.each').remove()};
						}
						main.doAjax(url, data, 'RECOVER_SUCCESS', callback);
					}
					else {
						alert('没有选取');
					}
					return false;
				}
			}
		};
		hack = {
			dataList : function(){
				$('#list .each input[type=checkbox]').removeClass('hide');
			}
		}
})